package com.wcity.manage.common.utils;

import java.util.UUID;

/**
 * 字符串操作辅助工具类
 *
 * @author dy
 */
public class StringUtil {
	
	/**
	 * 判断一个String对象是否为""
	 *
	 * @param s
	 * @return 为""返回true
	 */
	public static boolean isBlank( String s ) {
		return isNull(s) || s.isEmpty();
	}

	/**
	 * 判断一个String对象是否为NULL
	 *
	 * @param s
	 * @return 为NULL返回true
	 */
	public static boolean isNull( String s ) {
		return s == null;
	}
	/**
	 * 判断一个字符串是否是数字
	 *
	 * @param arg
	 * @return
	 */
	public static boolean isNumeric(String arg) {
		return isNumeric(arg, -1);
	}

	/**
	 * 判断一个字符串是否是特定长度
	 *
	 * @param arg
	 * @param len 长度
	 * @return
	 */
	public static boolean isNumeric(String arg, int... len) {

		if (isBlank(arg)) return false;
		
		String tmp = arg.trim();

		int i = 0;
		
		for (; i < tmp.length(); i++) {
			char one = tmp.charAt(i);
			
			if (one > '9' || one < '0') return false;
			
		}
		
		if (len.length > 0 && len[0] > 0 && i != len[0]) return false;
		
		return true;
	}
	
 	/**
  	 * 生成随机数
	 *
  	 * @param numberFlag true表示只生成数值
  	 * @param length 长度
  	 * @return
  	 */
	public static String createRandom(boolean numberFlag, int length) {
		StringBuilder retStr = new StringBuilder("");
		String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";
		int len = strTable.length();
		boolean bDone = true;
		do {
			int count = 0;
			for (int i = 0; i < length; i++) {
				double dblR = Math.random() * len;
				int intR = (int) Math.floor(dblR);
				char c = strTable.charAt(intR);
				if (('0' <= c) && (c <= '9')) {
					count++;
				}
				retStr.append( strTable.charAt(intR) );
			}
			if (count >= 2) {
				bDone = false;
			}
		} while (bDone);

		return retStr.toString();
	}

	/**
	 * 获取token
	 *
	 * @param str
	 * @return
	 */
	public static String getToken( String str ) {

		StringBuffer sb = new StringBuffer();
		sb.append( str ).append( "*_" ).append( System.currentTimeMillis() );
		sb.append( "`^" ).append( StringUtil.createRandom( true, 4 ) );
		return DigestUitl.MD5LowerCase( sb.toString() );
	}

	/**
	 * 获取UUID
	 *
	 * @return
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString().replace( '-', '0' );
	}

	/**
	 * 获取文件后缀名
	 *
	 * @param filename
	 * @return
	 */
	public static String getSuffix( String filename ) {
		if ( null == filename ) return "";
		if ( filename.isEmpty() || filename.lastIndexOf( "." ) < 0 || filename.endsWith( "." ) ) return "";
		String[] strs = filename.split( "\\." );
		return strs[ strs.length - 1 ];
	}

	/**
	 * 检测文件后缀
	 *
	 * @param oriFileName
	 * @param allow
	 * @return
	 */
	public static boolean checkSuffix( String oriFileName, String allow ) {
		return allow.indexOf( StringUtil.getSuffix(oriFileName) ) > -1;
	}

	/**
	 * 字符串处理
	 * @param str
	 * @return
	 */
	public static String digestStr(String str){

		if ( isBlank( str ) ) return "";

		String[] array = str.split(",");

		StringBuffer sb = new StringBuffer();
		for ( int i=0; i < array.length ; i++ ) {

			if ( isBlank( array[i] ) ) continue;

			if ( i == array.length-1 ) {
				sb.append( array[i] );
			} else {
				sb.append( array[i] ).append( "," );
			}

		}

		return sb.toString();
	}

}
